{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "dets = [[10,10,20,20,1],[15,15,15,15,1],[12,12,15,20,1],[14,12,16,20,1]]\n",
    "trks = [[11,11,19,19,0],[8,9,17,16,0],[13,13,21,22,0]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "bb_gt = np.expand_dims(trks,0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[11, 11, 19, 19,  0],\n",
       "        [ 8,  9, 17, 16,  0],\n",
       "        [13, 13, 21, 22,  0]]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bb_gt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "bb_test = np.expand_dims(dets,1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[10, 10, 20, 20,  1]],\n",
       "\n",
       "       [[15, 15, 15, 15,  1]],\n",
       "\n",
       "       [[12, 12, 15, 20,  1]],\n",
       "\n",
       "       [[14, 12, 16, 20,  1]]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bb_test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "xx1 = np.maximum(bb_test[..., 0], bb_gt[..., 0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[11, 10, 13],\n",
       "       [15, 15, 15],\n",
       "       [12, 12, 13],\n",
       "       [14, 14, 14]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xx1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(4, 3)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xx1.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "yy1 = np.maximum(bb_test[..., 1], bb_gt[..., 1])\n",
    "xx2 = np.minimum(bb_test[..., 2], bb_gt[..., 2])\n",
    "yy2 = np.minimum(bb_test[..., 3], bb_gt[..., 3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[11, 10, 13],\n",
       "       [15, 15, 15],\n",
       "       [12, 12, 13],\n",
       "       [12, 12, 13]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "yy1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[19, 17, 20],\n",
       "       [15, 15, 15],\n",
       "       [15, 15, 15],\n",
       "       [16, 16, 16]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xx2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[19, 16, 20],\n",
       "       [15, 15, 15],\n",
       "       [19, 16, 20],\n",
       "       [19, 16, 20]])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "yy2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "w = np.maximum(0., xx2 - xx1)\n",
    "h = np.maximum(0., yy2 - yy1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[8., 7., 7.],\n",
       "       [0., 0., 0.],\n",
       "       [3., 3., 2.],\n",
       "       [2., 2., 2.]])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "w"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[8., 6., 7.],\n",
       "       [0., 0., 0.],\n",
       "       [7., 4., 7.],\n",
       "       [7., 4., 7.]])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "h"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "wh = w*h"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[64., 42., 49.],\n",
       "       [ 0.,  0.,  0.],\n",
       "       [21., 12., 14.],\n",
       "       [14.,  8., 14.]])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "wh"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "o = wh/((bb_test[...,2] - bb_test[..., 0]) *\n",
    "              (bb_test[..., 3] - bb_test[..., 1]) +\n",
    "              (bb_gt[..., 2] - bb_gt[..., 0]) *\n",
    "              (bb_gt[..., 3] - bb_gt[..., 1]) - wh)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.64      , 0.34710744, 0.39837398],\n",
       "       [0.        , 0.        , 0.        ],\n",
       "       [0.31343284, 0.16      , 0.17073171],\n",
       "       [0.21212121, 0.11267606, 0.18918919]])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "o"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "colors = np.random.randint(0, 256, (32, 3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[240, 122,  30],\n",
       "       [153, 138,   8],\n",
       "       [ 27,  80, 110],\n",
       "       [ 47,  33, 198],\n",
       "       [237, 232, 246],\n",
       "       [  9, 245, 192],\n",
       "       [236,  30,  88],\n",
       "       [181, 153,  38],\n",
       "       [199, 184, 128],\n",
       "       [217,  75,  67],\n",
       "       [ 54, 154, 182],\n",
       "       [158, 111, 157],\n",
       "       [ 13, 222,  28],\n",
       "       [ 26, 145, 149],\n",
       "       [ 35,  87, 216],\n",
       "       [ 21,  29, 142],\n",
       "       [ 78, 143, 232],\n",
       "       [161,  79,  22],\n",
       "       [178,  54,  36],\n",
       "       [194,  48, 123],\n",
       "       [222,  29,   7],\n",
       "       [109,  96, 206],\n",
       "       [143,  19,  20],\n",
       "       [ 78, 166, 180],\n",
       "       [ 82, 152, 230],\n",
       "       [113, 220,  98],\n",
       "       [ 26, 244, 143],\n",
       "       [234,  16,  82],\n",
       "       [235,  17,   0],\n",
       "       [129,  66,  99],\n",
       "       [225,  75, 182],\n",
       "       [128,  65, 117]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "colors"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}